package com.bdqn.desiremall.serivce.shippingaddress;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bdqn.desiremall.entity.Mall_ShippingAddress;
import com.bdqn.desiremall.mapper.Mall_ShippingAddressMapper;
import com.desiremall.util.MyBatisUtil;

public class ShippingAddressSerivceImpl implements ShippingAddressService{
           SqlSession session = null;
	
   /**
    * UCO0701
    * �鿴�Ñ���؛��ַ���ջ���ַ���������롢�ֻ���롢�ջ��ˣ�
    * 
    */
	@Override
	public List<Mall_ShippingAddress> addressView(int userId) {
		//�û��ջ���ַ����
		List<Mall_ShippingAddress> findAddress = null;
		try {
			session = MyBatisUtil.getSession();
			//��ȡ�ջ���ַmapper
			Mall_ShippingAddressMapper mapper = session.getMapper(Mall_ShippingAddressMapper.class);
			 findAddress = mapper.findAddress(userId);
			
		} catch (Exception e) {
		e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(session);
		}
		
		return findAddress;
	}

	
	/**
	 * UCO0701
	 * ����ջ���ַ
	 */
	@Override
	public boolean addShippingAddress(Mall_ShippingAddress shippingAddress) {
		try {
			session = MyBatisUtil.getSession();
			//��ȡ�ջ���ַmapper
			Mall_ShippingAddressMapper mapper = session.getMapper(Mall_ShippingAddressMapper.class);
			 int i = mapper.addAddress(shippingAddress);
			 if (i>0) {
				session.commit();
				return true;
			}
			
		} catch (Exception e) {
			session.rollback();
		    e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}
    /**UCO0702
     * �޸��ջ���ַ
     */
	@Override
	public boolean changeAddress(Mall_ShippingAddress shippingAddress) {
		try {
			session = MyBatisUtil.getSession();
			//��ȡ�ջ���ַmapper
			Mall_ShippingAddressMapper mapper = session.getMapper(Mall_ShippingAddressMapper.class);
			 int i = mapper.modifyAddress(shippingAddress);
			 if (i>0) {
				session.commit();
				return true;
			}
			
		} catch (Exception e) {
			session.rollback();
		    e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
		
	}
   /**
    * UCO0703
    * ɾ���ջ���ַ
    */
	@Override
	public boolean moveAddress(int addressId) {
		try {
			session = MyBatisUtil.getSession();
			//��ȡ�ջ���ַmapper
			Mall_ShippingAddressMapper mapper = session.getMapper(Mall_ShippingAddressMapper.class);
			 int i = mapper.removeAddress(addressId);
			 if (i>0) {
				session.commit();
				return true;
			}
			
		} catch (Exception e) {
			session.rollback();
		    e.printStackTrace();
		}finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}

}
